from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from apps.app01.models import Product
from apps.app01.views.product_detail.serializer.config_serializer import ProductDetailSerializer


class ProductDetailView(APIView):
    authentication_classes = []
    def get(self, request):
        # 获取 query 参数中的 productId
        product_id = request.query_params.get('productId')

        # 如果 productId 存在，则获取单个产品
        if product_id:
            try:
                product = Product.objects.get(id=product_id)
                serializer = ProductDetailSerializer(product)
                return Response({
                    "success": True,
                    "data": serializer.data
                })
            except Product.DoesNotExist:
                return Response({
                    "success": False,
                    "message": "Product not found"
                }, status=status.HTTP_404_NOT_FOUND)
        else:
            return Response({
                "success": False,
                "message": "ProductId is required"
            }, status=status.HTTP_400_BAD_REQUEST)